﻿using Ninject;
using Ninject.Parameters;
using Ninject.Syntax;
using System.Collections.Concurrent;

namespace HobSoft.SoccerBet2014.Domain.Common.Factory
{
    using Domain.Core;
    using Domain.DataService;
    using Domain.Factory;
    using Models;
    public class BfManagerFactory : IBfManagerFactory
    {
        private static ConcurrentDictionary<string, IBfManagerService> bfManagerImpl = new ConcurrentDictionary<string, IBfManagerService>();
        private readonly IResolutionRoot _resolutionRoot;

        public BfManagerFactory(IResolutionRoot resolutionRoot)
        {
            _resolutionRoot = resolutionRoot;
        }

        public IBfManagerService CreateOrGet(BFAccount account)
        {
            if (bfManagerImpl.ContainsKey(account.Account))
                return bfManagerImpl[account.Account];
            var impl = (IBfManagerService)_resolutionRoot.Get(typeof(BfService.BfManager)
                                    , new ConstructorArgument("account", account)
                                    , new ConstructorArgument("_bfDataService", _resolutionRoot.Get<IBfEventDataService>()));
            bfManagerImpl.TryAdd(account.Account, impl);
            return bfManagerImpl[account.Account];
        }

        public IBfManagerService CreateOrGet()
        {
            return _resolutionRoot.Get<IBfManagerService>();
        }

        object IServiceFactory.CreateOrGet()
        {
            return _resolutionRoot.Get<IBfManagerService>();
        }
    }
}
